const fs=require('fs');
const {join}=require('path')
const chalk=require('chalk')
class Animal{
    constructor(){
        this.timer=null;
        this.currentTime=+new Date();
        this.currentIndex=0;
        this.initColor="#ccccc";
        this.animalList=[];
        this.date=0;
    }
    init(){
        this.getAnimallist();
        this.draw();
    }
    getAnimallist(){//获取animal列表
        let result=fs.readFileSync(join(__dirname,'../../../animal图表.txt'),'utf-8');
        this.animalList=result.split('===============++++SEPERATOR++++====================');
        //  console.log(this.animalList.length)
    }

    randomColor(){//随机颜色
      this.initColor= '#'+Math.floor((Math.random()*1111111)).toString(16);
      return this.initColor
    // console.log(this.initColor);
    }

    randomIndex(){//随机下标
        this.currentIndex=Math.floor(Math.random()*this.animalList.length-1)

        return this.currentIndex
    }

    draw(){//展示
            this.timer=setInterval(()=>{
                this.date= ++this.date
                console.log(chalk.hex(this.randomColor())(this.date),chalk.hex(this.randomColor())(this.animalList[this.randomIndex()]));
                
                if(this.date==10){
                    clearInterval(this.timer)
                }
               
            },1000)
            
        }

    

};

module.exports = new Animal();